﻿using System;
using System.Drawing;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.Data.SqlClient;
namespace QLTV.Forms.KeHoach
{
    public partial class frmTrainingPlan : BaseForm
    {
        public frmTrainingPlan()
        {
            InitializeComponent();
            this.statusDataBar1.addTextChange(gridView1);
        }

        private void frmTrainingPlan_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'dsTraining.tblTraningPlan' table. You can move, or remove it, as needed.
            this.tblTraningPlanTableAdapter.Fill(this.dsTraining.tblTraningPlan);

        }

        private void statusDataBar1_NewClick(object sender, StatusDataBar.StatusDataBarArgs e)
        {
            this.tblTraningPlanBindingSource.AddNew();
        }

        private void statusDataBar1_UpdateClick(object sender, StatusDataBar.StatusDataBarArgs e)
        {
            this.tblTraningPlanBindingSource.EndEdit();
            this.tblTraningPlanTableAdapter.Update(dsTraining);
            this.tblTraningPlanTableAdapter.Fill(this.dsTraining.tblTraningPlan);
            alertControl1.Show(this, rm.GetString("sMessageCaption", myCultureInfo), rm.GetString("sMessageSuccess", myCultureInfo));
        }

        private void statusDataBar1_UndoClick(object sender, StatusDataBar.StatusDataBarArgs e)
        {
            
            this.tblTraningPlanBindingSource.CancelEdit();
            this.tblTraningPlanTableAdapter.Fill(this.dsTraining.tblTraningPlan);
        }

        private void statusDataBar1_DeleteClick(object sender, StatusDataBar.StatusDataBarArgs e)
        {
            if (XtraMessageBox.Show(rm.GetString("sConfirm", myCultureInfo), rm.GetString("sMessageCaption", myCultureInfo), MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.Yes)
            {
                try
                {
                    this.tblTraningPlanBindingSource.RemoveCurrent();
                    this.tblTraningPlanTableAdapter.Update(dsTraining);
                    this.tblTraningPlanTableAdapter.Fill(this.dsTraining.tblTraningPlan);
                }
                catch (SqlException ex)
                {
                    this.tblTraningPlanTableAdapter.Fill(this.dsTraining.tblTraningPlan);
                    if (ex.ErrorCode == -2146232060)
                    {
                        XtraMessageBox.Show(rm.GetString("sDataInUsed", myCultureInfo), rm.GetString("sMessageCaption", myCultureInfo), MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                    else
                    {
                        XtraMessageBox.Show(ex.Message, rm.GetString("sMessageCaption", myCultureInfo), MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
            }
        }

        private void gridView1_DoubleClick(object sender, EventArgs e)
        {
            //frmTrainingCrew f = new frmTrainingCrew();
            //f.StartPosition = FormStartPosition.CenterScreen;
            //f.TrainingPlanId = int.Parse(gridView1.GetFocusedRowCellValue(colnTrainingPlanId).ToString());
            //f.CreatedDate = DateTime.Parse(gridView1.GetFocusedRowCellValue(coldCreateDate).ToString());
            //f.ShowDialog();
        }

        private void statusDataBar1_PrintClick(object sender, StatusDataBar.StatusDataBarArgs e)
        {

            if (!string.IsNullOrEmpty(gridView1.GetFocusedRowCellValue(coldCreateDate).ToString()))
            {
                QLTV.Reports.rptCountTrainingCrewCourse r = new Reports.rptCountTrainingCrewCourse();
                r.CreatedDate = DateTime.Parse(gridView1.GetFocusedRowCellValue(coldCreateDate).ToString());
                r.ShowPreview();
            }
            else
            {
                XtraMessageBox.Show("Dữ liệu chưa cập nhật ngày tháng", "Thông tin", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void btnShowPlan_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            //frmTrainingCrew f = new frmTrainingCrew();
            //f.StartPosition = FormStartPosition.CenterScreen;
            //f.TrainingPlanId = int.Parse(gridView1.GetFocusedRowCellValue(colnTrainingPlanId).ToString());
            //f.CreatedDate = DateTime.Parse(gridView1.GetFocusedRowCellValue(coldCreateDate).ToString());
            //f.ShowDialog();
        }

        private void gridControl1_MouseUp(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                popupMenu1.ShowPopup(new Point(Cursor.Position.X, Cursor.Position.Y));
            }
        }
    }
}